<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>方法定义与使用 | 浪飞IT小栈</title>
    <meta name="generator" content="VuePress 1.9.9">
    <link rel="icon" href="/img/favicon.ico">
    <meta name="description" content="前后端技术分享">
    <meta name="keywords" content="前端博客,后端博客,人工智能,个人技术博客,前端,前端开发,前端框架,web前端,前端面试题,技术文档,学习,面试,JavaScript,js,ES6,TypeScript,vue,python,css3,html5,Node,git,github,markdown,java,Spring,Springboot,Redis,mysql">
    <meta name="baidu-site-verification" content="7F55weZDDc">
    <meta name="theme-color" content="#11a8cd">
    
    <link rel="preload" href="/assets/css/0.styles.ada71c49.css" as="style"><link rel="preload" href="/assets/js/app.a2a00aa5.js" as="script"><link rel="preload" href="/assets/js/2.256f807a.js" as="script"><link rel="preload" href="/assets/js/17.1d678834.js" as="script"><link rel="prefetch" href="/assets/js/10.ab144fe3.js"><link rel="prefetch" href="/assets/js/11.062e0879.js"><link rel="prefetch" href="/assets/js/12.f9c60882.js"><link rel="prefetch" href="/assets/js/13.0a6ebfcf.js"><link rel="prefetch" href="/assets/js/14.03fc6f12.js"><link rel="prefetch" href="/assets/js/15.19dbf9fd.js"><link rel="prefetch" href="/assets/js/16.6e68160b.js"><link rel="prefetch" href="/assets/js/18.4536fd5c.js"><link rel="prefetch" href="/assets/js/19.9c1b65df.js"><link rel="prefetch" href="/assets/js/20.c5f96e71.js"><link rel="prefetch" href="/assets/js/21.3f0b334e.js"><link rel="prefetch" href="/assets/js/22.55c868be.js"><link rel="prefetch" href="/assets/js/23.91286241.js"><link rel="prefetch" href="/assets/js/24.c1af1909.js"><link rel="prefetch" href="/assets/js/25.a995a755.js"><link rel="prefetch" href="/assets/js/26.abc5dea3.js"><link rel="prefetch" href="/assets/js/27.1eb62850.js"><link rel="prefetch" href="/assets/js/28.b383a871.js"><link rel="prefetch" href="/assets/js/29.088e0d73.js"><link rel="prefetch" href="/assets/js/3.cd716564.js"><link rel="prefetch" href="/assets/js/30.3ccc98d5.js"><link rel="prefetch" href="/assets/js/31.aab7cc23.js"><link rel="prefetch" href="/assets/js/32.5e16b7e6.js"><link rel="prefetch" href="/assets/js/33.4aaeb741.js"><link rel="prefetch" href="/assets/js/34.45e8e898.js"><link rel="prefetch" href="/assets/js/35.5899c97a.js"><link rel="prefetch" href="/assets/js/36.32e2e263.js"><link rel="prefetch" href="/assets/js/37.c4e27bbd.js"><link rel="prefetch" href="/assets/js/38.fe5e60d2.js"><link rel="prefetch" href="/assets/js/39.2cb0b4ee.js"><link rel="prefetch" href="/assets/js/4.edf4d5e7.js"><link rel="prefetch" href="/assets/js/40.08d5bead.js"><link rel="prefetch" href="/assets/js/41.2f80f24b.js"><link rel="prefetch" href="/assets/js/42.43a49913.js"><link rel="prefetch" href="/assets/js/43.08da7ec2.js"><link rel="prefetch" href="/assets/js/44.7c7cb8a9.js"><link rel="prefetch" href="/assets/js/45.0ddf7e7f.js"><link rel="prefetch" href="/assets/js/46.7c4609a5.js"><link rel="prefetch" href="/assets/js/47.edf3ddf8.js"><link rel="prefetch" href="/assets/js/48.569a90b9.js"><link rel="prefetch" href="/assets/js/49.fa368ac8.js"><link rel="prefetch" href="/assets/js/5.24054156.js"><link rel="prefetch" href="/assets/js/50.b9b3ea7f.js"><link rel="prefetch" href="/assets/js/51.a2ebf0fe.js"><link rel="prefetch" href="/assets/js/52.31f02238.js"><link rel="prefetch" href="/assets/js/53.4110ef1e.js"><link rel="prefetch" href="/assets/js/54.dab741c7.js"><link rel="prefetch" href="/assets/js/55.f27984e9.js"><link rel="prefetch" href="/assets/js/56.bd4c1a8e.js"><link rel="prefetch" href="/assets/js/57.375d40cb.js"><link rel="prefetch" href="/assets/js/58.932e3864.js"><link rel="prefetch" href="/assets/js/59.e657be1b.js"><link rel="prefetch" href="/assets/js/6.b259c061.js"><link rel="prefetch" href="/assets/js/60.88d94274.js"><link rel="prefetch" href="/assets/js/61.fa71d884.js"><link rel="prefetch" href="/assets/js/62.a9d3b3b7.js"><link rel="prefetch" href="/assets/js/63.f849fe75.js"><link rel="prefetch" href="/assets/js/64.1770f7a9.js"><link rel="prefetch" href="/assets/js/65.6afc33db.js"><link rel="prefetch" href="/assets/js/66.799f2eb4.js"><link rel="prefetch" href="/assets/js/67.f36b2f7d.js"><link rel="prefetch" href="/assets/js/68.868cd00b.js"><link rel="prefetch" href="/assets/js/69.4c29b907.js"><link rel="prefetch" href="/assets/js/7.e34ea00f.js"><link rel="prefetch" href="/assets/js/70.1b993ed4.js"><link rel="prefetch" href="/assets/js/71.e16ad4f9.js"><link rel="prefetch" href="/assets/js/72.627505e6.js"><link rel="prefetch" href="/assets/js/73.32768e14.js"><link rel="prefetch" href="/assets/js/74.968a580d.js"><link rel="prefetch" href="/assets/js/75.42d5af3d.js"><link rel="prefetch" href="/assets/js/76.19a69d31.js"><link rel="prefetch" href="/assets/js/77.cf627a78.js"><link rel="prefetch" href="/assets/js/78.07177880.js"><link rel="prefetch" href="/assets/js/8.c4dd00ce.js"><link rel="prefetch" href="/assets/js/9.0ff73be2.js">
    <link rel="stylesheet" href="/assets/css/0.styles.ada71c49.css">
  </head>
  <body class="theme-mode-light">
    <div id="app" data-server-rendered="true"><div class="theme-container sidebar-open have-rightmenu"><header class="navbar blur"><div title="目录" class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/img/logo.png" alt="浪飞IT小栈" class="logo"> <span class="site-name can-hide">浪飞IT小栈</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link">首页</a></div><div class="nav-item"><a href="/pages/6138ae/" class="nav-link">Java速通</a></div><div class="nav-item"><a href="/pages/705b35/" class="nav-link">面试经典</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="精品项目" class="dropdown-title"><a href="/project/" class="link-title">精品项目</a> <span class="title" style="display:none;">精品项目</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>桌面端</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/pages/8143cc480faf9a11/" class="nav-link">JavaScript</a></li></ul></li><li class="dropdown-item"><h4>PC后端</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/note/javascript/" class="nav-link">项目1</a></li><li class="dropdown-subitem"><a href="/note/typescript-axios/" class="nav-link">项目二</a></li></ul></li></ul></div></div><div class="nav-item"><a href="/about/" class="nav-link">电子书</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="索引" class="dropdown-title"><a href="/archives/" class="link-title">索引</a> <span class="title" style="display:none;">索引</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/" class="nav-link">分类</a></li><li class="dropdown-item"><!----> <a href="/tags/" class="nav-link">标签</a></li><li class="dropdown-item"><!----> <a href="/archives/" class="nav-link">归档</a></li></ul></div></div><div class="nav-item"><a href="/pages/0c07b2/" class="nav-link">IT杂货铺</a></div><div class="nav-item"><a href="/archives/" class="nav-link">更多资源</a></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <div class="sidebar-hover-trigger"></div> <aside class="sidebar" style="display:none;"><div class="blogger"><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/avatar.jpg"> <div class="blogger-info"><h3>浪飞yes</h3> <span>无法简单的人儿~</span></div></div> <nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link">首页</a></div><div class="nav-item"><a href="/pages/6138ae/" class="nav-link">Java速通</a></div><div class="nav-item"><a href="/pages/705b35/" class="nav-link">面试经典</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="精品项目" class="dropdown-title"><a href="/project/" class="link-title">精品项目</a> <span class="title" style="display:none;">精品项目</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>桌面端</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/pages/8143cc480faf9a11/" class="nav-link">JavaScript</a></li></ul></li><li class="dropdown-item"><h4>PC后端</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/note/javascript/" class="nav-link">项目1</a></li><li class="dropdown-subitem"><a href="/note/typescript-axios/" class="nav-link">项目二</a></li></ul></li></ul></div></div><div class="nav-item"><a href="/about/" class="nav-link">电子书</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="索引" class="dropdown-title"><a href="/archives/" class="link-title">索引</a> <span class="title" style="display:none;">索引</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/" class="nav-link">分类</a></li><li class="dropdown-item"><!----> <a href="/tags/" class="nav-link">标签</a></li><li class="dropdown-item"><!----> <a href="/archives/" class="nav-link">归档</a></li></ul></div></div><div class="nav-item"><a href="/pages/0c07b2/" class="nav-link">IT杂货铺</a></div><div class="nav-item"><a href="/archives/" class="nav-link">更多资源</a></div> <!----></nav>  <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>开篇</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>基础语法</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/pages/a5bdcf/" class="sidebar-link">计算机导论与环境配置</a></li><li><a href="/pages/32e9a2/" class="sidebar-link">Java开发入门与数据类型</a></li><li><a href="/pages/0ccc48/" class="sidebar-link">变量和运算符</a></li><li><a href="/pages/67fd43/" class="sidebar-link">选择语句和循环语句</a></li><li><a href="/pages/92a804/" class="sidebar-link">一维数组</a></li><li><a href="/pages/0840c0/" aria-current="page" class="active sidebar-link">方法定义与使用</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level2"><a href="/pages/0840c0/#一、今日学习内容和目标" class="sidebar-link">一、今日学习内容和目标</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/0840c0/#_1-1-学习内容" class="sidebar-link">1.1 学习内容</a></li><li class="sidebar-sub-header level3"><a href="/pages/0840c0/#_1-2-今日学习目标" class="sidebar-link">1.2 今日学习目标</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/0840c0/#二、方法引入" class="sidebar-link">二、方法引入</a></li><li class="sidebar-sub-header level2"><a href="/pages/0840c0/#三、方法定义和调用" class="sidebar-link">三、方法定义和调用</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/0840c0/#_3-1-方法-method-的定义-重点" class="sidebar-link">3.1 方法(Method)的定义（重点）</a></li><li class="sidebar-sub-header level3"><a href="/pages/0840c0/#_3-2-方法调用-重点" class="sidebar-link">3.2 方法调用（重点）</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/0840c0/#四、方法设计练习" class="sidebar-link">四、方法设计练习</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/0840c0/#_4-1-方法设计练习-掌握" class="sidebar-link">4.1 方法设计练习（掌握）</a></li><li class="sidebar-sub-header level3"><a href="/pages/0840c0/#_4-2-方法重载-了解" class="sidebar-link">4.2 方法重载（了解）</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/0840c0/#五、方法参数的值传递" class="sidebar-link">五、方法参数的值传递</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/0840c0/#_5-1-基本类型参数-理解" class="sidebar-link">5.1 基本类型参数（理解）</a></li><li class="sidebar-sub-header level3"><a href="/pages/0840c0/#_5-2-引用类型参数-理解" class="sidebar-link">5.2 引用类型参数（理解）</a></li><li class="sidebar-sub-header level3"><a href="/pages/0840c0/#_5-3-debug模式-会用" class="sidebar-link">5.3 Debug模式 （会用）</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/0840c0/#六、小结与作业" class="sidebar-link">六、小结与作业</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/0840c0/#_6-1-小结" class="sidebar-link">6.1 小结</a></li><li class="sidebar-sub-header level3"><a href="/pages/0840c0/#_6-2-作业" class="sidebar-link">6.2 作业</a></li><li class="sidebar-sub-header level4"><a href="/pages/0840c0/#_6-2-1-题目1" class="sidebar-link">6.2.1 题目1</a></li><li class="sidebar-sub-header level4"><a href="/pages/0840c0/#_6-2-2-题目2" class="sidebar-link">6.2.2 题目2</a></li><li class="sidebar-sub-header level4"><a href="/pages/0840c0/#_6-2-3-题目3" class="sidebar-link">6.2.3 题目3</a></li><li class="sidebar-sub-header level4"><a href="/pages/0840c0/#_6-2-4-题目4" class="sidebar-link">6.2.4 题目4</a></li><li class="sidebar-sub-header level4"><a href="/pages/0840c0/#_6-2-5-题目5" class="sidebar-link">6.2.5 题目5</a></li><li class="sidebar-sub-header level4"><a href="/pages/0840c0/#_6-2-6-题目6" class="sidebar-link">6.2.6 题目6</a></li><li class="sidebar-sub-header level4"><a href="/pages/0840c0/#_6-2-7-题目7" class="sidebar-link">6.2.7 题目7</a></li><li class="sidebar-sub-header level4"><a href="/pages/0840c0/#_6-2-8-题目8" class="sidebar-link">6.2.8 题目8</a></li><li class="sidebar-sub-header level4"><a href="/pages/0840c0/#_6-2-9-题目9" class="sidebar-link">6.2.9 题目9</a></li><li class="sidebar-sub-header level4"><a href="/pages/0840c0/#_6-2-10-题目10" class="sidebar-link">6.2.10 题目10</a></li></ul></li></ul></li></ul></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>面向对象</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>常用类</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>基础高级</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>数据库</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>前端技术</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>热门框架</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>前后端分离</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>权限控制</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>NoSQL</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>微服务</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>消息中间件</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>脚手架</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>技术增值</span> <span class="arrow right"></span></p> <!----></section></li></ul> </aside> <div><main class="page"><div class="theme-vdoing-wrapper "><div class="articleInfo-wrap" data-v-06225672><div class="articleInfo" data-v-06225672><ul class="breadcrumbs" data-v-06225672><li data-v-06225672><a href="/" title="首页" class="iconfont icon-home router-link-active" data-v-06225672></a></li> <li data-v-06225672><a href="/categories/?category=Java%E9%80%9F%E9%80%9A" title="分类" data-v-06225672>Java速通</a></li><li data-v-06225672><a href="/categories/?category=%E5%9F%BA%E7%A1%80%E8%AF%AD%E6%B3%95" title="分类" data-v-06225672>基础语法</a></li></ul> <div class="info" data-v-06225672><div title="作者" class="author iconfont icon-touxiang" data-v-06225672><a href="https://github.com/langfeiyes" target="_blank" title="作者" class="beLink" data-v-06225672>langfeiyes</a></div> <div title="创建时间" class="date iconfont icon-riqi" data-v-06225672><a href="javascript:;" data-v-06225672>2024-02-28</a></div> <!----></div></div></div> <!----> <div class="content-wrapper"><div class="right-menu-wrapper"><div class="right-menu-margin"><div class="right-menu-title">目录</div> <div class="right-menu-content"></div></div></div> <h1><img src="">方法定义与使用<!----></h1>  <div class="theme-vdoing-content content__default"><h1 id="方法定义与使用"><a href="#方法定义与使用" class="header-anchor">#</a> 方法定义与使用</h1> <h2 id="一、今日学习内容和目标"><a href="#一、今日学习内容和目标" class="header-anchor">#</a> 一、今日学习内容和目标</h2> <h3 id="_1-1-学习内容"><a href="#_1-1-学习内容" class="header-anchor">#</a> 1.1 学习内容</h3> <ul><li>方法的作用</li> <li>方法的定义和调用</li> <li>方法的设计练习</li> <li>方法的重载</li> <li>方法的值传递机制</li></ul> <h3 id="_1-2-今日学习目标"><a href="#_1-2-今日学习目标" class="header-anchor">#</a> 1.2 今日学习目标</h3> <ul><li><p>了解方法的作用</p></li> <li><p>掌握方法的定义和调用</p></li> <li><p>通过多个案例掌握方法定义和调用</p></li> <li><p>了解方法重载的作用和判断规则</p></li> <li><p>理解参数的值传递机制</p></li></ul> <h2 id="二、方法引入"><a href="#二、方法引入" class="header-anchor">#</a> 二、方法引入</h2> <p>需求 :  求一个数组中所有元素的和</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">MethodDemo</span> <span class="token punctuation">{</span>
	<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// 需求 :  求一个数组中所有元素的和</span>
		<span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> arr <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">7</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
        <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>i <span class="token operator">&lt;</span> arr<span class="token punctuation">.</span>length<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">int</span> ele <span class="token operator">=</span> arr<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>
            sum <span class="token operator">+=</span> ele<span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        
        <span class="token comment">// 如果还有一个数组，需要求和怎么办？</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><p>如果还有一个数组，需要求和怎么办？</p> <p><strong>在开发中我们要遵循DRY原则（Don’t Repeat Yourself）——不要重复你自己的代码</strong>。</p> <p>因为重复意味着<strong>维护成本很大</strong>，如果要修改功能代码，则每一个重复的地方都要修改一次，如果工程很大，你敢确保每个重复的地方都能改到吗？何况，你不感到厌烦吗？不无聊吗？</p> <p>​		<strong>为了解决功能代码重复编写的问题</strong>，可以把求和功能提取出来专门放在一个代码块（一对{ }）中，并为这段代码起个唯一的名字，如此，每次需要数组求和时直接通过这个名字调用求和功能就可以了。</p> <blockquote><p>意识：  把重复的功能代码抽离出来，定义成方法 , 通过方法调用完成重复功能地执行。</p></blockquote> <p>所以，方法的作用是？解决了功能代码的重复使用的问题</p> <h2 id="三、方法定义和调用"><a href="#三、方法定义和调用" class="header-anchor">#</a> 三、方法定义和调用</h2> <h3 id="_3-1-方法-method-的定义-重点"><a href="#_3-1-方法-method-的定义-重点" class="header-anchor">#</a> 3.1 方法(Method)的定义（重点）</h3> <p>方法：<strong>完成某一特定功能（如：求和，统计数量等）且可以被重复调用的代码块</strong></p> <p>定义方法，语法格式：</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token punctuation">[</span>修饰符<span class="token punctuation">]</span> 返回值类型 方法名称<span class="token punctuation">(</span>参数类型 参数名<span class="token number">1</span><span class="token punctuation">,</span>参数类型 参数名<span class="token number">2</span>，<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// 方法体</span>
    <span class="token punctuation">[</span><span class="token keyword">return</span> 返回值<span class="token punctuation">;</span><span class="token punctuation">]</span>
<span class="token punctuation">}</span> 
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>看看最熟悉的main方法的定义。</p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/3xiYXwrOoNflUEq.png" alt=""></p> <p>格式分析：</p> <ul><li><p>修饰符：public、static等, <font color="blue">static修饰的方法直接使用类名调用即可，目前都使用public static修饰</font></p></li> <li><p><font color="blue">返回值类型：限定返回值的类型，方法在完成一个功能后，是否需要给调用者返回一个结果？</font></p> <ul><li><font color="blue">如果需要给调用者返回结果，就写上返回数据的类型</font></li> <li><font color="blue">如果不需要给调用者返回结果，此时使用关键字void，表示无返回值</font></li></ul></li> <li><p>方法名称：用于调用方法，遵循标识符规范，建议动词开头，首字母小写，采用驼峰命名法</p></li> <li><p><font color="blue">参数列表：方法圆括号中的变量，可以有多个</font>，多个参数使用，号分割。</p></li> <li><p>方法体：方法的{ ... }中的代码，编写如何完成该功能的代码</p></li> <li><p>return关键字：在方法体中使用return关键字</p> <ul><li>功能1：把值返回给该方法调用者，此时该方法不能使用void修饰</li> <li>功能2：结束当前方法</li></ul></li></ul> <p><strong>注意事项：</strong></p> <ul><li><p>方法必须定义在类中，在Java中最小的程序单元是类，必须先有类</p></li> <li><p>一个类中可以定义多个方法</p></li> <li><p>方法和方法是平行的，不能在方法中定义另一个方法</p></li> <li><p>方法的定义没有先后顺序</p></li></ul> <h3 id="_3-2-方法调用-重点"><a href="#_3-2-方法调用-重点" class="header-anchor">#</a> 3.2 方法调用（重点）</h3> <p>需求：在MethodDemo类中，定义一个求两个整数之和的方法</p> <p><strong>方法定义分析：</strong></p> <ul><li><p>求两个数之和，到底是哪两个整数？说明存在两个未知的整数，需要定义变量来接收</p></li> <li><p>该方法要求求两个数之和，必定需要给调用者返回一个结果，否则，毫无意义。</p></li></ul> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">int</span> <span class="token function">getSum</span><span class="token punctuation">(</span><span class="token keyword">int</span> a<span class="token punctuation">,</span> <span class="token keyword">int</span> b<span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token keyword">int</span> c <span class="token operator">=</span> a <span class="token operator">+</span> b<span class="token punctuation">;</span>
	<span class="token keyword">return</span> c<span class="token punctuation">;</span>	<span class="token comment">//返回 a + b之和</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p><strong>注意：</strong></p> <ul><li><p>方法中的int a和int b就是<strong>形式参数</strong>（简称形参），即使参数名不叫a和b，也不影响，仅仅是变量名而已。</p></li> <li><p>方法定义出来，必须要调用才能生效。</p></li></ul> <p><strong>方法调用：</strong></p> <ul><li>如果方法有返回类型，此时需要返回类型定义变量，接收方法返回的结果，并打印，这才有意义。</li> <li>调用方法的地方，可以称之为调用处 ( 或调用者 )</li> <li><strong>实际参数</strong>：在调用某一个具体方法时，实际传递的参数值、
<ul><li>调用方法时，传递的参数2和3就是实际参数，简称实参。</li></ul></li></ul> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token comment">// 语法格式：</span>
返回值类型 变量 <span class="token operator">=</span> 方法所在类名<span class="token punctuation">.</span>方法名（实际参数）<span class="token punctuation">;</span>

<span class="token keyword">int</span> result <span class="token operator">=</span> <span class="token class-name">MethodDemo</span><span class="token punctuation">.</span><span class="token function">getSum</span><span class="token punctuation">(</span><span class="token number">2</span> <span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>result<span class="token punctuation">)</span><span class="token punctuation">;</span>		<span class="token comment">//输出5</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>完整代码如下：</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">MethodDemo</span> <span class="token punctuation">{</span>
	<span class="token comment">// 定义求两个整数之和的方法</span>
	<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">int</span> <span class="token function">getSum</span><span class="token punctuation">(</span><span class="token keyword">int</span> a<span class="token punctuation">,</span> <span class="token keyword">int</span> b<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token keyword">int</span> c <span class="token operator">=</span> a <span class="token operator">+</span> b<span class="token punctuation">;</span>
		<span class="token keyword">return</span> c<span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
	<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token comment">// 调用MethodDemo类中的getSum方法,传入2和3两个参数值,并接收方法返回的结果</span>
		<span class="token keyword">int</span> result <span class="token operator">=</span> <span class="token class-name">MethodDemo</span><span class="token punctuation">.</span><span class="token function">getSum</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>result<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><p>画图分析：</p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/XkPzlrBT6MDCQ5o.png" alt=""></p> <p><strong>方法调用时，把实参的值按顺序赋值给对应的形参</strong></p> <h2 id="四、方法设计练习"><a href="#四、方法设计练习" class="header-anchor">#</a> 四、方法设计练习</h2> <p>如何定义一个方法，① 主要是确定有没有参数，是什么参数，②有没有返回值，返回什么，这得结合具体需求来确定。</p> <p>那么，这两个要点如何确定？</p> <ul><li><p>形式参数：完成一个功能，是否存在哪些未知的因素<strong>需要调用者传入</strong>，把它们作为方法定义时的参数(形式参数)</p></li> <li><p>返回值类型：完成一个功能，要不要给调用者返回一个结果?</p> <ul><li>如果需要给调用者返回结果，就写上结果的类型</li> <li>如果不需要给调用者返回结果，此时使用关键字void，表示无返回</li></ul></li></ul> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token operator">?</span> 方法名<span class="token punctuation">(</span><span class="token operator">?</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h3 id="_4-1-方法设计练习-掌握"><a href="#_4-1-方法设计练习-掌握" class="header-anchor">#</a> 4.1 方法设计练习（掌握）</h3> <p><strong>需求1：定义一个方法，在控制台打印指定行数的指定字符串</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token comment">// 参数：打印的行数和字符串都是未知的，需要调用者传入，所以设置两个形参来接收调用者传递进来的实参</span>
<span class="token comment">// 返回值类型：因为只要求在控制台打印指定的字符串， 所以，该方法不需要返回值，void</span>
<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">print</span><span class="token punctuation">(</span><span class="token keyword">int</span> row<span class="token punctuation">,</span> <span class="token class-name">String</span> output<span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator">&lt;=</span> row<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>output<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p>调用方法：</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// static修饰的方法调用：类名.方法名（实参）</span>
	<span class="token class-name">MethodDemo</span><span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token string">&quot;你好师姐&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token class-name">MethodDemo</span><span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token string">&quot;你好龙哥&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p><strong>需求2：定义一个方法，传入一个int数组，按照指定格式(格式:[x,x,x]) 在控制台打印int类型数组</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token comment">// 参数：打印的数据所在的数组是未知的，所以定义一个形参来接收调用者传递进来的实参</span>
<span class="token comment">// 返回值类型：因为只要求打印指定的字符串， 所以，该方法不需要返回值，void</span>

<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">printArray</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> arr<span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token class-name">String</span> str <span class="token operator">=</span> <span class="token string">&quot;[&quot;</span><span class="token punctuation">;</span>
	<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> arr<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token keyword">int</span> item <span class="token operator">=</span> arr<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>
        str <span class="token operator">+=</span> item<span class="token punctuation">;</span>
		<span class="token keyword">if</span> <span class="token punctuation">(</span>i <span class="token operator">==</span> arr<span class="token punctuation">.</span>length <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
			str <span class="token operator">+=</span> <span class="token string">&quot;]&quot;</span><span class="token punctuation">;</span>
		<span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
			str <span class="token operator">+=</span> <span class="token string">&quot;, &quot;</span><span class="token punctuation">;</span>
		<span class="token punctuation">}</span>
	<span class="token punctuation">}</span>
	<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><p>调用方法：</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> arr <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token punctuation">{</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">9</span> <span class="token punctuation">}</span><span class="token punctuation">;</span>
	<span class="token class-name">MethodDemo</span><span class="token punctuation">.</span><span class="token function">printArray</span><span class="token punctuation">(</span>arr<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p><strong>需求3：定义一个方法，传入一个int数组，返回指定元素在数组中第一次出现的索引</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token comment">// 参数：数组和要找的元素是未知的，所以定义两个形参来接收调用者传递的实参</span>
<span class="token comment">// 返回值类型：因为要求返回元素的索引，所以返回值的类型应该为int</span>
<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">int</span> <span class="token function">indexOf</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> arr<span class="token punctuation">,</span> <span class="token keyword">int</span> key<span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> arr<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token keyword">if</span> <span class="token punctuation">(</span>arr<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">==</span> key<span class="token punctuation">)</span> <span class="token punctuation">{</span>
			<span class="token keyword">return</span> i<span class="token punctuation">;</span>
		<span class="token punctuation">}</span>
	<span class="token punctuation">}</span>
	<span class="token keyword">return</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span><span class="token comment">// 如果没找到， 返回-1</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><p>调用方法：</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> arr <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token punctuation">{</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">9</span> <span class="token punctuation">}</span><span class="token punctuation">;</span>
	<span class="token keyword">int</span> index <span class="token operator">=</span> <span class="token class-name">MethodDemo</span><span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span>arr<span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>index<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p><strong>需求4：定义一个方法，传入一个int数组，返回该数组所有元素之和的平均数</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">double</span> <span class="token function">getAvg</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> arr<span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token keyword">double</span> avg <span class="token operator">=</span> <span class="token number">0.0</span><span class="token punctuation">;</span>
    <span class="token keyword">int</span> sum <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
    <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> item<span class="token operator">:</span>arr<span class="token punctuation">)</span><span class="token punctuation">{</span>
        sum <span class="token operator">+=</span> item<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    avg <span class="token operator">=</span> sum <span class="token operator">*</span> <span class="token number">1.0</span> <span class="token operator">/</span> arr<span class="token punctuation">.</span>length<span class="token punctuation">;</span>
    <span class="token keyword">return</span> avg<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><p>调用方法：</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> arr <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token punctuation">{</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">9</span> <span class="token punctuation">}</span><span class="token punctuation">;</span>
	<span class="token keyword">double</span> avg <span class="token operator">=</span> <span class="token class-name">MethodDemo</span><span class="token punctuation">.</span><span class="token function">getAvg</span><span class="token punctuation">(</span>arr<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>avg<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p><strong>练习需求5：定义一个方法，传入两个参数，一个double类型的折扣，一个double数组表示多个货品的原价格，返回支付货品的总价格</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">double</span> <span class="token function">getTotalPrice</span><span class="token punctuation">(</span><span class="token keyword">double</span> cutOff<span class="token punctuation">,</span><span class="token keyword">double</span><span class="token punctuation">[</span><span class="token punctuation">]</span> arr<span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token keyword">double</span> total <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
	<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">double</span> ele <span class="token operator">:</span> arr<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		total <span class="token operator">=</span> total <span class="token operator">+</span> ele<span class="token punctuation">;</span><span class="token comment">// 求和</span>
	<span class="token punctuation">}</span>
	<span class="token keyword">return</span> total <span class="token operator">*</span> cutOff<span class="token punctuation">;</span>	<span class="token comment">// 返回折后总价</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p>调用方法：</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token keyword">double</span><span class="token punctuation">[</span><span class="token punctuation">]</span> arr <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token keyword">double</span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token punctuation">{</span> <span class="token number">10.0</span><span class="token punctuation">,</span> <span class="token number">30.0</span><span class="token punctuation">,</span> <span class="token number">50.0</span><span class="token punctuation">,</span> <span class="token number">70.0</span> <span class="token punctuation">}</span><span class="token punctuation">;</span>
	<span class="token keyword">double</span> totalPrice <span class="token operator">=</span> <span class="token class-name">MethodDemo</span><span class="token punctuation">.</span><span class="token function">getTotalPrice</span><span class="token punctuation">(</span><span class="token number">0.8</span><span class="token punctuation">,</span>arr<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>totalPrice<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p><strong>需求6：定义一个方法，传入一个数组，获取数组的最大值和最小值</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">MethodDemo9</span> <span class="token punctuation">{</span>

    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token function">getMaxMin</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> arr<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// 1 动态初始化长度为2的int数组 newArr</span>
        <span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> newArr <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token keyword">int</span><span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
        <span class="token comment">// 2 求最大值, 存入newArr的0号元素中</span>
        <span class="token keyword">int</span> max <span class="token operator">=</span> arr<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
        <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> ele <span class="token operator">:</span> arr<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">if</span><span class="token punctuation">(</span>ele <span class="token operator">&gt;</span> max<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                max <span class="token operator">=</span> ele<span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
        newArr<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">=</span> max<span class="token punctuation">;</span>

        <span class="token comment">// 3 求最小值, 存入newArr的1号元素中</span>
        <span class="token keyword">int</span> min <span class="token operator">=</span> arr<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
        <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> ele <span class="token operator">:</span> arr<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">if</span><span class="token punctuation">(</span>ele <span class="token operator">&lt;</span> min<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                min <span class="token operator">=</span> ele<span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
        newArr<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">=</span> min<span class="token punctuation">;</span>

        <span class="token comment">// 4 返回newArr</span>
        <span class="token keyword">return</span> newArr<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">/**
         * 总结:
         * 方法只能返回一个值，如果返回多个同类型的值，把这些值存入数组，然后返回数组
         * return 数组名;
         */</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br></div></div><h3 id="_4-2-方法重载-了解"><a href="#_4-2-方法重载-了解" class="header-anchor">#</a> 4.2 方法重载（了解）</h3> <p>方法的重载（overload），在同一类中，方法名称相同，但参数列表不同的多个方法构成方法重载关系。</p> <p><strong>方法重载判断原则：两同一不同</strong></p> <ul><li>在同一个类中，方法名是相同的。</li> <li>方法参数列表不同。只要<strong>参数类型、参数个数、参数类型的顺序</strong>任意一个不同，就叫参数列表不同</li></ul> <p>注意：方法重载和方法的返回值类型无关</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">OverLoadDemo</span> <span class="token punctuation">{</span>
    
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">int</span> <span class="token function">getSum</span><span class="token punctuation">(</span><span class="token keyword">int</span> a<span class="token punctuation">,</span><span class="token keyword">int</span> b<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> a <span class="token operator">+</span> b<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">double</span> <span class="token function">getSum</span><span class="token punctuation">(</span><span class="token keyword">double</span> a<span class="token punctuation">,</span><span class="token keyword">double</span> b<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> a <span class="token operator">+</span> b<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">int</span> <span class="token function">getSum</span><span class="token punctuation">(</span><span class="token keyword">int</span> a<span class="token punctuation">,</span><span class="token keyword">int</span> b<span class="token punctuation">,</span><span class="token keyword">int</span> c<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> a <span class="token operator">+</span> b <span class="token operator">+</span> c<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">double</span> <span class="token function">getSum</span><span class="token punctuation">(</span><span class="token keyword">int</span> a<span class="token punctuation">,</span> <span class="token keyword">double</span> b<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> a <span class="token operator">+</span> b<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">double</span> <span class="token function">getSum</span><span class="token punctuation">(</span><span class="token keyword">double</span> a<span class="token punctuation">,</span> <span class="token keyword">int</span> b<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> a <span class="token operator">+</span> b<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">OverLoadDemo</span><span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">OverLoadDemo</span><span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2.0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">OverLoadDemo</span><span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token number">1.0</span><span class="token punctuation">,</span><span class="token number">2.0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br></div></div><p>方法重载的作用：解决了同一功能的方法由于参数不同所造成方法名称不同。</p> <p>实际开发中，需要更简单、快速的识别方法重载  —— <strong>方法签名</strong></p> <ul><li>方法签名：方法名称  +  参数类型，在同一个类中，方法签名必须是唯一的，否则编译报错。</li></ul> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">MethodSignDemo</span> <span class="token punctuation">{</span>

    <span class="token comment">// 方法的签名 add(int,int)</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">int</span> <span class="token function">getSum</span><span class="token punctuation">(</span><span class="token keyword">int</span> a<span class="token punctuation">,</span><span class="token keyword">int</span> b<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> a <span class="token operator">+</span> b<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><p>有了方法签名，重载：<strong>在同一类中，方法名称相同，但方法签名不同的多个方法构成方法重载。</strong></p> <p>练习：以下的方法都在同一个类中，且方法名都为doWork。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token comment">// doWork(int,char,boolean)</span>
<span class="token keyword">void</span> <span class="token function">doWork</span><span class="token punctuation">(</span><span class="token keyword">int</span> a<span class="token punctuation">,</span><span class="token keyword">char</span> b<span class="token punctuation">,</span><span class="token keyword">boolean</span> c<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token punctuation">}</span>
下列方法哪些是上述doWork方法的重载方法。
<span class="token comment">// doWork(char,int,boolean)</span>
<span class="token keyword">void</span> <span class="token function">doWork</span><span class="token punctuation">(</span><span class="token keyword">char</span> b<span class="token punctuation">,</span> <span class="token keyword">int</span> a<span class="token punctuation">,</span> <span class="token keyword">boolean</span> c<span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span> <span class="token comment">// YES</span>
<span class="token comment">// doWork(boolean,char,int)</span>
<span class="token keyword">int</span> <span class="token function">doWork</span><span class="token punctuation">(</span><span class="token keyword">boolean</span> a<span class="token punctuation">,</span><span class="token keyword">char</span> c <span class="token punctuation">,</span><span class="token keyword">int</span> b<span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span>	<span class="token comment">// YES</span>
<span class="token comment">// doWork(int,char,double)</span>
<span class="token keyword">void</span> <span class="token function">doWork</span><span class="token punctuation">(</span><span class="token keyword">int</span> a<span class="token punctuation">,</span><span class="token keyword">char</span> b<span class="token punctuation">,</span><span class="token keyword">double</span> c<span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span>	<span class="token comment">// YES</span>
<span class="token comment">// doWork(int,char,boolean)</span>
<span class="token keyword">void</span> <span class="token function">doWork</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">,</span><span class="token keyword">char</span> y<span class="token punctuation">,</span><span class="token keyword">boolean</span> z<span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span>	<span class="token comment">// 编译错误</span>
<span class="token comment">// doWork(int,double)</span>
<span class="token keyword">int</span> <span class="token function">doWork</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">,</span><span class="token keyword">double</span> y<span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span>	        <span class="token comment">// YES</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br></div></div><h2 id="五、方法参数的值传递"><a href="#五、方法参数的值传递" class="header-anchor">#</a> 五、方法参数的值传递</h2> <p>值传递：方法在调用时，把实参的值  <strong>复制一份</strong>  赋值给形参的过程称为值传递。</p> <p>java方法调用时的参数传递过程就采用值传递。</p> <h3 id="_5-1-基本类型参数-理解"><a href="#_5-1-基本类型参数-理解" class="header-anchor">#</a> 5.1 基本类型参数（理解）</h3> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">change</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;change before,x=&quot;</span> <span class="token operator">+</span> x<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//10</span>
	x <span class="token operator">=</span> <span class="token number">100</span><span class="token punctuation">;</span><span class="token comment">// 修改x变量的值</span>
	<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;change after,x=&quot;</span> <span class="token operator">+</span> x<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//100</span>
<span class="token punctuation">}</span>

<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token keyword">int</span> a <span class="token operator">=</span> <span class="token number">10</span><span class="token punctuation">;</span>
	<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;main change before,a=&quot;</span> <span class="token operator">+</span> a<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//10</span>
	<span class="token function">change</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;main change after,a=&quot;</span> <span class="token operator">+</span> a<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//100? 10?</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><p>输出结果：</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code>main before<span class="token punctuation">,</span>a<span class="token operator">=</span><span class="token number">10</span>
change before<span class="token punctuation">,</span>x<span class="token operator">=</span><span class="token number">10</span>
change after<span class="token punctuation">,</span>x<span class="token operator">=</span><span class="token number">100</span>
main after<span class="token punctuation">,</span>a<span class="token operator">=</span><span class="token number">10</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/zwdRQo2hyr5nvxN.png" alt=""></p> <p>当把main方法中a变量的值复制一份，传递给change方法，main方法中的a变量的值不受改变。</p> <p><strong>结论：基本数据类型参数,在方法的调用过程中，形参的改变，不影响实参</strong></p> <h3 id="_5-2-引用类型参数-理解"><a href="#_5-2-引用类型参数-理解" class="header-anchor">#</a> 5.2 引用类型参数（理解）</h3> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">change</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> arr<span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;change before,arr[0]=&quot;</span> <span class="token operator">+</span> arr<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//10</span>
	arr<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">30</span><span class="token punctuation">;</span><span class="token comment">// 修改数组中第一个元素的值</span>
	<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;change after,x[0]=&quot;</span> <span class="token operator">+</span> arr<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//30</span>
<span class="token punctuation">}</span>

<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> a <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token punctuation">{</span> <span class="token number">10</span><span class="token punctuation">,</span><span class="token number">90</span> <span class="token punctuation">}</span><span class="token punctuation">;</span>
	<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;main change before,a[0]=&quot;</span> <span class="token operator">+</span> a<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//10</span>
	<span class="token function">change</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;main change after,a[0]=&quot;</span> <span class="token operator">+</span> a<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//30</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><p>输出结果：</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code>main before<span class="token punctuation">,</span>a<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token operator">=</span><span class="token number">10</span>
change before<span class="token punctuation">,</span>arr<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token operator">=</span><span class="token number">10</span>
change after<span class="token punctuation">,</span>x<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token operator">=</span><span class="token number">30</span>
main after<span class="token punctuation">,</span>a<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token operator">=</span><span class="token number">30</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/lch8konIWwjuagM.png" alt=""></p> <p><strong>结论：引用数据类型参数,在方法的调用过程中，形参的改变，影响实参</strong></p> <p>小结：</p> <p>传递基本类型参数：传递参数<strong>值</strong></p> <p>传递引用类型参数：传递参数所引用的堆空间地址值</p> <h3 id="_5-3-debug模式-会用"><a href="#_5-3-debug模式-会用" class="header-anchor">#</a> 5.3 Debug模式 （会用）</h3> <p>Debug调试: 跟踪程序的每一步的运行过程</p> <p>断点: 让程序停在某一行</p> <p>如何debug调试：</p> <ul><li><p>打断点</p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/FpOKSVJLhABjoex.png" alt=""></p></li> <li><p>进入debug模式</p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/FTQwy9kiqHLb8M7.png" alt=""></p></li> <li><p>点击单步调试按钮</p></li></ul> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/erLMNSxmdqPDoZv.png" alt=""></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">DebugDemo</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">int</span> a <span class="token operator">=</span> <span class="token number">10</span><span class="token punctuation">;</span>
        <span class="token keyword">int</span> b <span class="token operator">=</span> <span class="token number">20</span><span class="token punctuation">;</span>
        <span class="token keyword">int</span> c <span class="token operator">=</span> a <span class="token operator">+</span> b<span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;c = &quot;</span> <span class="token operator">+</span> c<span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> arr <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token number">10</span><span class="token punctuation">,</span><span class="token number">20</span><span class="token punctuation">,</span><span class="token number">30</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
        <span class="token keyword">int</span> sum <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
        <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> arr<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">int</span> ele <span class="token operator">=</span> arr<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>
            sum <span class="token operator">+=</span> ele<span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;sum = &quot;</span> <span class="token operator">+</span> sum<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><p>今日快捷键</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token comment">// xx.var + tab 快速定义变量</span>
快速向上移动代码<span class="token operator">:</span> ctrl <span class="token operator">+</span> shift <span class="token operator">+</span> 向上箭头 
快速向上移动代码<span class="token operator">:</span> ctrl <span class="token operator">+</span> shift <span class="token operator">+</span> 向下箭头 
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h2 id="六、小结与作业"><a href="#六、小结与作业" class="header-anchor">#</a> 六、小结与作业</h2> <h3 id="_6-1-小结"><a href="#_6-1-小结" class="header-anchor">#</a> 6.1 小结</h3> <ul><li><p>方法的作用</p></li> <li><p>main和其他方法的关系</p></li> <li><p>定义方法的语法</p></li> <li><p>方法的返回值类型如何决定？</p></li> <li><p>方法应该有什么形式参数如何决定？ 未知因素</p></li> <li><p>调用方法的语法，为什么这里可以使用类去调用方法？</p></li> <li><p>实际参数和形式参数的关系</p></li> <li><p>如何理解可变参数</p></li> <li><p>方法重载的判断依据</p></li> <li><p>基本类型和引用类型的参数传递的区别（画图说明）</p></li></ul> <h3 id="_6-2-作业"><a href="#_6-2-作业" class="header-anchor">#</a> 6.2 作业</h3> <h4 id="_6-2-1-题目1"><a href="#_6-2-1-题目1" class="header-anchor">#</a> 6.2.1 题目1</h4> <p><strong>需求：定义求两个整数之和的方法</strong></p> <p>步骤：</p> <p>（1）定义方法</p> <p>​			确定方法名，修饰符（暂时使用static）</p> <p>​			确定是否需要形式参数</p> <p>​			确定是否需要返回</p> <p>（2）调用方法</p> <p>​			确定调用语法（暂时类名调用方法）</p> <p>​			确定需要传入什么实际参数</p> <p>​			确定是否需要定义变量接收返回值</p> <h4 id="_6-2-2-题目2"><a href="#_6-2-2-题目2" class="header-anchor">#</a> 6.2.2 题目2</h4> <p><strong>需求：定义一个方法，打印指定多少行的指定字符串</strong></p> <p>步骤：</p> <p>（1）定义方法</p> <p>（2）实现方法体</p> <p>（3）调用方法</p> <h4 id="_6-2-3-题目3"><a href="#_6-2-3-题目3" class="header-anchor">#</a> 6.2.3 题目3</h4> <p><strong>需求：定义一个方法，传入一个int数组，按照格式打印int类型数组</strong></p> <p>步骤：</p> <p>（1）定义方法</p> <p>（2）实现方法体</p> <p>（3）调用方法</p> <h4 id="_6-2-4-题目4"><a href="#_6-2-4-题目4" class="header-anchor">#</a> 6.2.4 题目4</h4> <p><strong>需求：定义一个方法，传入一个int数组，返回指定元素在数组中第一次出现的索引</strong></p> <p>步骤：</p> <p>（1）定义方法</p> <p>（2）实现方法体</p> <p>遍历数组</p> <p>如果当前元素和指定元素相同，返回当前元素所在的索引</p> <p>遍历所有元素后，发现找不到则返回-1，表示找不到索引</p> <p>（3）调用方法</p> <h4 id="_6-2-5-题目5"><a href="#_6-2-5-题目5" class="header-anchor">#</a> 6.2.5 题目5</h4> <p><strong>需求:定义一个方法getSum,计算两个整数数组的总和</strong></p> <h4 id="_6-2-6-题目6"><a href="#_6-2-6-题目6" class="header-anchor">#</a> 6.2.6 题目6</h4> <p><strong>需求:定义一个方法printArray,按照格式打印数组中非0的元素,例如{1,0,2,3,0,4,5}打印出来应该为<code>[1,2,3,4,5]</code></strong></p> <h4 id="_6-2-7-题目7"><a href="#_6-2-7-题目7" class="header-anchor">#</a> 6.2.7 题目7</h4> <p><strong>需求:定义一个方法getFactorial,求出n!的结果.阶乘公式为<code>n! = 1*2*...*n</code>.</strong></p> <h4 id="_6-2-8-题目8"><a href="#_6-2-8-题目8" class="header-anchor">#</a> 6.2.8 题目8</h4> <p><strong>需求：定义一个方法接收一个 int 类型的二维数组，返回所有元素平均值(int 类型)。</strong></p> <h4 id="_6-2-9-题目9"><a href="#_6-2-9-题目9" class="header-anchor">#</a> 6.2.9 题目9</h4> <p><strong>需求：定义一个名为combineArrays的方法，以两个int类型的数组arr1和arr2作为参数，实现将arr1和arr2两个数组合并为一个新的一维数组，并返回合并后的新数组。</strong></p> <p>例如：arr1={1,2},arr2={3,4}，则该方法应该返回{1,2,3,4}。</p> <p>思路：</p> <p>（1）先求出符合要求的元素个数</p> <p>（2）创建一个新的数组存放符合要求的元素</p> <p>（3）将符合要求的元素存放到新的数组中</p> <h4 id="_6-2-10-题目10"><a href="#_6-2-10-题目10" class="header-anchor">#</a> 6.2.10 题目10</h4> <p><strong>需求：定义一个方法getAvg, 求出某个数组去掉最大值和最小值之后的平均分.</strong></p> <p>思路：</p> <ol><li><p>遍历数组，求得最大值和最小值</p></li> <li><p>重新遍历数组，统计不等于最大值也不等于最小值的元素的和和个数</p></li> <li><p>计算平均分并返回</p></li></ol></div></div>  <div class="page-edit"><!----> <div class="tags"><a href="/tags/?tag=methed" title="标签">#methed</a></div> <!----></div> <div class="page-nav-wapper"><div class="page-nav-centre-wrap"><a href="/pages/92a804/" class="page-nav-centre page-nav-centre-prev"><div class="tooltip">一维数组</div></a> <a href="/pages/294c58/" class="page-nav-centre page-nav-centre-next"><div class="tooltip">类与对象和包</div></a></div> <div class="page-nav"><p class="inner"><span class="prev">
        ←
        <a href="/pages/92a804/" class="prev">一维数组</a></span> <span class="next"><a href="/pages/294c58/">类与对象和包</a>→
      </span></p></div></div></div> <div class="article-list"><div class="article-title"><a href="/archives/" class="iconfont icon-bi">最近更新</a></div> <div class="article-wrapper"><dl><dd>01</dd> <dt><a href="/pages/0c07b2/"><div>
            IT杂货铺
            <!----></div></a> <span class="date">03-23</span></dt></dl><dl><dd>02</dd> <dt><a href="/pages/705b35/"><div>
            人事百问
            <!----></div></a> <span class="date">03-22</span></dt></dl><dl><dd>03</dd> <dt><a href="/pages/78eb56/"><div>
            MyBatis-Plus
            <!----></div></a> <span class="date">03-12</span></dt></dl> <dl><dd></dd> <dt><a href="/archives/" class="more">更多文章&gt;</a></dt></dl></div></div></main></div> <div class="footer"><div class="icons"><a href="mailto:langfeiyes@163.com" title="发邮件" target="_blank" class="iconfont icon-youjian"></a><a href="https://github.com/langfeiyes" title="GitHub" target="_blank" class="iconfont icon-github"></a><a href="https://music.163.com/#/playlist?id=755597173" title="听音乐" target="_blank" class="iconfont icon-erji"></a></div> 
  Theme by
  <a href="https://github.com/xugaoyi/vuepress-theme-vdoing" target="_blank" title="本站主题">Vdoing</a> 
    | Copyright © 2024-2024
    <span><a href="https://beian.miit.gov.cn/" target="_blank">粤ICP备18007927号-1</a></span></div> <div class="buttons"><div title="返回顶部" class="button blur go-to-top iconfont icon-fanhuidingbu" style="display:none;"></div> <div title="去评论" class="button blur go-to-comment iconfont icon-pinglun" style="display:none;"></div> <div title="主题模式" class="button blur theme-mode-but iconfont icon-zhuti"><ul class="select-box" style="display:none;"><li class="iconfont icon-zidong">
          跟随系统
        </li><li class="iconfont icon-rijianmoshi">
          浅色模式
        </li><li class="iconfont icon-yejianmoshi">
          深色模式
        </li><li class="iconfont icon-yuedu">
          阅读模式
        </li></ul></div></div> <!----> <!----> <!----></div><div class="global-ui"><div></div></div></div>
    <script src="/assets/js/app.a2a00aa5.js" defer></script><script src="/assets/js/2.256f807a.js" defer></script><script src="/assets/js/17.1d678834.js" defer></script>
  </body>
</html>
